//
// Created by 11510 on 2025/10/2.
//

#include "GetCommand.h"
#include "RegistryContainer.hpp"
#include "CameraApp.h"
xclib_client::GetCommand::GetCommand(CommandContext ctx)
        : ICommand("get", ctx) {

};

std::optional<std::future<int>> xclib_client::GetCommand::run(const std::vector<std::string> &args, bool wait) {
    cxxopts::Options options("get", "get");
    options.add_options()
            ("u,unit", "unit", cxxopts::value<uint16_t>()->default_value(""))
            ("r,register", "register name", cxxopts::value<std::string>()->default_value(""))
            ("help", "help");
    try {
        auto res = parseOptions(options, args);
        if (res.count("help")) {
            std::cout << options.help() << "\n";
            return std::nullopt;
        }
        auto u = res["unit"].as<uint16_t>();
        auto r = res["register"].as<std::string>();
        auto task = [u, r]() {
            std::cout << "Get '" << r << "' from '" << u << "'...\n";
            registry::RegistryContainer::instance().get<CameraApp>("lazy")->get(u, r);
            return 0;
        };
        if (!wait) return make_optional(std::async(std::launch::async, task));
        else {
            task();
            return std::nullopt;
        }
    } catch (const cxxopts::exceptions::exception &e) {
        std::cerr << "get: " << e.what() << "\n";
        return std::nullopt;
    }
}

std::string xclib_client::GetCommand::shortHelp() const { return "get -u <unitmap> -r <register name>"; }

std::string xclib_client::GetCommand::longHelp() const { return "get -u <unitmap> -r <register name>\n    read register"; }